<template>
    <div class="warehouse-details">
        <el-drawer v-model="dialogVisible" title="入库记录详情" size="1200px" :with-header="false" direction="rtl">
            <div class="drawer-header">
                <div class="title">{{ dialogRow.no || 'IN202507071505000532' }}</div>
                <!-- <el-button plain @click="dialogVisible = false">
                    <el-icon>
                        <Close />
                    </el-icon>
                </el-button> -->
            </div>

            <div class="drawer-content">
                <div class="tab-header">
                    <div class="warehouse-info">
                        <div class="info-item">
                            <div class="label">订单类型</div>
                            <div class="value">{{ props.dialogRow.source_type_text || '采购单' }}</div>
                        </div>
                        <div class="info-item">
                            <div class="label">关联采购/出库退货单</div>
                            <div class="value">{{ props.dialogRow.source_no || 'PO2025070415300001894' }}</div>
                        </div>
                        <div class="info-item">
                            <div class="label">入库仓库</div>
                            <div class="value">{{ props.dialogRow.warehouse?.name || '成都王仓库' }}</div>
                        </div>
                        <div class="info-item">
                            <div class="label">入库时间</div>
                            <div class="value">{{ props.dialogRow.in_date || '2025-06-30 00:00:00' }}</div>
                        </div>
                        <div class="info-item">
                            <div class="label">负责人员</div>
                            <div class="value">{{ props.dialogRow.warehouse?.manager_name || '仓库负责人1' }}</div>
                        </div>
                        <div class="info-item">
                            <div class="label">备注</div>
                            <div class="value">{{ props.dialogRow.note || '测试' }}</div>
                        </div>
                    </div>
                </div>

                <div class="tab-container">
                    <el-tabs v-model="activeTab">
                        <el-tab-pane label="基础信息" name="basic">
                            <div class="info-section">
                                <div class="section-title">
                                    <el-icon>
                                        <ArrowDown />
                                    </el-icon>
                                    <span>基础信息</span>
                                </div>
                                <div class="info-grid">
                                    <div class="info-row">
                                        <div class="info-col">
                                            <span class="label">入库单号</span>
                                            <span class="value">{{ props.dialogRow.no || 'IN202507071505000532' }}</span>
                                        </div>
                                        <div class="info-col">
                                            <span class="label">入库类型</span>
                                            <span class="value">{{ props.dialogRow.source_type_text || '采购单' }}</span>
                                        </div>
                                    </div>
                                    <div class="info-row">
                                        <div class="info-col">
                                            <span class="label">关联编号</span>
                                            <span class="value">{{ props.dialogRow.source_no || 'PO2025070415300001894' }}</span>
                                        </div>
                                        <div class="info-col">
                                            <span class="label">入库仓库</span>
                                            <span class="value">{{ props.dialogRow.warehouse?.name || '成都王仓库' }}</span>
                                        </div>
                                    </div>
                                    <div class="info-row">
                                        <div class="info-col">
                                            <span class="label">仓库编号</span>
                                            <span class="value">{{ props.dialogRow.warehouse?.code || '100001' }}</span>
                                        </div>
                                        <div class="info-col">
                                            <span class="label">库房类型</span>
                                            <span class="value">{{ props.dialogRow.warehouse?.type_text || '电器库房' }}</span>
                                        </div>
                                    </div>
                                    <div class="info-row">
                                        <div class="info-col">
                                            <span class="label">入库时间</span>
                                            <span class="value">{{ props.dialogRow.in_date || '2025-06-30 00:00:00' }}</span>
                                        </div>
                                        <div class="info-col">
                                            <span class="label">库房人员</span>
                                            <span class="value">{{ props.dialogRow.warehouse?.manager_name || '仓库负责人1' }}</span>
                                        </div>
                                    </div>
                                    <div class="info-row">
                                        <div class="info-col">
                                            <span class="label">联系电话</span>
                                            <span class="value">{{ props.dialogRow.warehouse?.manager_phone || '13898889661' }}</span>
                                        </div>
                                        <div class="info-col">
                                            <span class="label">库房地址</span>
                                            <span class="value">{{ props.dialogRow.warehouse?.address || '联系地址1' }}</span>
                                        </div>
                                    </div>
                                    <div class="info-row">
                                        <div class="info-col">
                                            <span class="label">备注</span>
                                            <span class="value">{{ props.dialogRow.note || '测试' }}</span>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="info-section">
                                <div class="section-title">
                                    <el-icon>
                                        <ArrowDown />
                                    </el-icon>
                                    <span>系统信息</span>
                                </div>
                                <div class="info-grid">
                                    <div class="info-row">
                                        <div class="info-col">
                                            <span class="label">创建人</span>
                                            <span class="value">{{ props.dialogRow.creator_name || '张三' }}</span>
                                        </div>
                                        <div class="info-col">
                                            <span class="label">创建时间</span>
                                            <span class="value">{{ props.dialogRow.created_at || '2025-07-07 15:05:38' }}</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </el-tab-pane>
           
                        <el-tab-pane label="入库产品" name="inbound-record">
                            <div class="record-content">
                                <div class="search-bar">
                                    <el-input v-model="recordSearch" placeholder="产品名称/规格型号" style="width: 300px" @input="handleSearch">
                                        <template #prefix>
                                            <el-icon>
                                                <Search />
                                            </el-icon>
                                        </template>
                                    </el-input>
                                </div>
                                <div class="inbound-list">
                                    <el-table :data="filteredProductList" border style="width: 100%;">
                                        <el-table-column label="产品名称" prop="product_name" />
                                        <el-table-column label="规格型号" prop="specification" />
                                        <el-table-column label="单位" prop="measure_unit_name" width="80" align="center" />
                                        <el-table-column label="本次入库数量" prop="in_num" align="center" width="120" />
                                        <el-table-column label="备注" prop="note" align="center" />
                                    </el-table>
                                </div>
                            </div>
                        </el-tab-pane>
                    </el-tabs>
                </div>
            </div>
        </el-drawer>
    </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { ArrowDown, Close, Search, RefreshRight, Download, Plus, Expand, Fold } from '@element-plus/icons-vue';

const props = defineProps({
    dialogVisible: {
        type: Boolean,
        default: false
    },
    dialogRow: {
        type: Object,
        default: () => ({})
    }
});

const emit = defineEmits(['update:dialogVisible']);

const dialogVisible = computed({
    get: () => props.dialogVisible,
    set: (val) => emit('update:dialogVisible', val)
});

watch(dialogVisible, (val) => {
    if(val){
        productList.value = props.dialogRow.product_in_item;
    }
});

const activeTab = ref('basic');
const recordSearch = ref('');

// 入库产品列表数据
const productList = ref([

]);

// 搜索功能
const filteredProductList = computed(() => {
    if (!recordSearch.value) {
        return productList.value;
    }
    const searchTerm = recordSearch.value.toLowerCase();
    // 容错处理，避免item为undefined或属性不存在时报错
    return productList.value.filter((item: any) => {
        const name = (item.product_name || '').toString().toLowerCase();
        const spec = (item.specification || '').toString().toLowerCase();
        console.log(name, spec, 'name, spec');
        return name.includes(searchTerm) || spec.includes(searchTerm);
    });
});

const handleSearch = () => {
    // 可以在这里添加额外的搜索逻辑
    console.log('搜索关键词:', recordSearch.value);
};

</script>

<style lang="scss" scoped>
::v-deep .inventory-layout .product-category .category-list .el-menu-item.is-active {
    color: #6860FF !important;
    background-color: #6860ff15 !important;
}

//表格样式
::v-deep .product-list {
    .el-table {
        --el-table-header-bg-color: rgba(235, 236, 240, 1);

        th {
            color: #444;
            font-weight: bold;
        }
    }
}

//表格样式
::v-deep .inbound-list {
    .el-table {
        --el-table-header-bg-color: rgba(235, 236, 240, 1);

        th {
            color: #444;
            font-weight: bold;
        }
    }
}

//分页样式
.pagination {
    width: calc(100% - 1000px);
    position: fixed;
    bottom: 20px;
    right: 20px;
    padding-top: 10px;
}

.warehouse-details {
    .drawer-header {
        padding: 16px 20px;
        border-bottom: 1px solid #E4E7ED;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .title {
            font-size: 16px;
            font-weight: bold;
            color: #333;
        }
    }

    .drawer-content {
        height: calc(100% - 60px);
        overflow-y: auto;

        .tab-header {
            padding: 20px;
            background-color: #F5F7FA;

            .warehouse-info {
                display: flex;
                flex-wrap: wrap;
                gap: 16px;

                .info-item {
                    min-width: 200px;

                    .label {
                        color: #909399;
                        margin-right: 8px;
                        margin-bottom: 4px;
                        font-size: 14px;
                    }

                    .value {
                        color: #333;
                        font-weight: 500;
                        font-size: 14px;
                    }
                }
            }
        }

        .tab-container {
            padding: 20px;

            :deep(.el-tabs__header) {
                margin-bottom: 20px;
            }
        }

        .info-section {
            margin-bottom: 24px;

            .section-title {
                background-color: #F5F7FA;
                padding: 12px 10px;
                display: flex;
                align-items: center;
                gap: 8px;
                margin-bottom: 16px;
                font-size: 14px;
                font-weight: bold;
                color: #333;

                .el-icon {
                    font-size: 16px;
                    color: #626aef;
                }
            }

            .info-grid {
                padding: 16px;
                border-radius: 4px;

                .info-row {
                    display: flex;
                    margin-bottom: 16px;

                    &:last-child {
                        margin-bottom: 0;
                    }

                    .info-col {
                        flex: 1;
                        display: flex;
                        align-items: center;

                        &.full-width {
                            flex: 2;
                        }

                        .label {
                            width: 120px;
                            color: #909399;
                            flex-shrink: 0;
                            margin-left: 10px;
                        }

                        .value {
                            color: #333;
                            flex: 1;
                            display: inline-block;
                            padding: 6px;
                            font-size: 14px;
                            background-color: #F5F7FA;
                        }
                    }
                }
            }
        }
    }
}

:deep(.el-drawer) {
    .el-drawer__body {
        padding: 0;
    }
}

:deep(.el-tabs__nav-wrap::after) {
    height: 1px;
}

:deep(.el-tabs__active-bar) {
    background-color: #626aef;
}

:deep(.el-tabs__item.is-active) {
    color: #626aef;
}

:deep(.el-tabs__item:hover) {
    color: #626aef;
}

.search-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;

    .right-buttons {
        display: flex;
        gap: 8px;
    }

    .filter-buttons {
        margin-left: 16px;
    }
}

.product-list,
.inbound-list,
.record-list {
    margin-bottom: 16px;
}

.pagination {
    display: flex;
    justify-content: flex-end;
    padding: 16px 0;
}

:deep(.el-table) {
    --el-table-header-bg-color: #F5F7FA;

    th {
        background-color: var(--el-table-header-bg-color);
        color: #606266;
        font-weight: 500;
    }
}

:deep(.el-button) {
    &.is-plain {
        &:hover {
            color: #626aef;
            border-color: #626aef;
        }
    }
}

:deep(.el-radio-button__orig-radio:checked + .el-radio-button__inner) {
    background-color: #626aef;
    border-color: #626aef;
    box-shadow: -1px 0 0 0 #626aef;
}
</style>
